package uebung;

import java.util.ArrayList;

public class Backpack {

	private int _maxValue; // maximum Value
	private int _maxWeight; // maximum weight for the load of the backpack
	private int _currWeight;
	private int _currValue;
	private ArrayList<Item> _load = new ArrayList<Item>();

	public Backpack(int _maxWeight) {
		this._maxWeight = _maxWeight;
	}

	public int getMaxValue() {
		return _maxValue;
	}

	public int getMaxLoad() {
		return _maxWeight;
	}

	public void setMaxLoad(int _maxLoad) {
		this._maxWeight = _maxLoad;
	}

	public ArrayList<Item> getLoad() {
		return _load;
	}

	public void addItem(Item _load) {

		if (_currWeight + _load.getWeight() <= _maxWeight) {
			_currWeight = _currWeight + _load.getWeight();
			_currValue = _currValue + _load.getValue();
			this._load.add(_load);
			System.out.println("Item eingepackt");
		} else {
			System.out.println("kein platz mehr!");
		}

	}

	public void addRandom(int _tries) {
		int _itemAmount = 100; // amount of different items
		int counter = 0; // counts the iterations
		ArrayList<Item> _itemPool = new ArrayList<Item>(); // a pool of
															// different items
		for (int i = 0; i <= _itemAmount; i++) {
			_itemPool.add(Item.getRandomItem());
		}
		for (int j = 0; j <= _tries; j++) {
			do {
				addItem(_itemPool.get((int) Math.random() * _itemAmount));
				counter++;
			} while (_currWeight < _maxWeight);

			_currWeight = 0;
			_currValue = 0;
		}

	}

	@Override
	public String toString() {
		return "Backpack [_maxWeight=" + _maxWeight + ", _currWeight="
				+ _currWeight + ", _currValue=" + _currValue + "]";
	}

}
